<?php
/**
 * 商品管理
 * @author hushuai
 *
 */
class ProductAction extends CommonAction {
	 
	private $tableDishes           = "Dishes";             //定义菜品表
	private $tableDrink            = "Drink";              //定义饮品表
	private $tableRdrelation       = "Rdrelation";         //定义餐厅菜品关联表
	private $tableDishsort         = "Dishsort";           //定义菜品分类表
	private $tableOutletDishesView = "OutletDishesView";   //定义餐厅菜品关联视图
	private $DishesSortView        = "DishesSortView";     //定义菜品fenlei关联视图
 
    /**
     * 初始化
     */
    public function _initialize(){
    
    	parent::_initialize();
    	//parent::checkTimestamp();
    }
    
    /**
     * 饮品列表
     */
    public function drink(){
		
		$cat['city_id']   = $this->_req("city_id","intval",0);
    	$cat['addid']     = $this->_req("addid","intval",0);
    	$cat['role_view'] = $this->_req("role_view","intval",0);
		
		if($cat['role_view']==1){
			$map['addid'] = $cat['addid'];	
		}
		
    	$desc      = $this->_get("orderDirection","trim","desc");;
    	$order     = $this->_get("orderName","trim","addtime");
    	$pageNum   = $this->_get("pageNum","intval",C('PAGE_NUMS'));
    	$nowPage   = $this->_get("nowPage","intval",1);
    	$where     = $this->_req("where");
    	$package   = $this->_get("package","intval",0);
    	
    	if($where['keywords']){
    		$map['title|entitle|drink_num'] = array("like","%".$where['keywords']."%");
    	}
    	 
    	if($where['type']){
    		$map['type'] = $where['type'];
    	}
    	 
    	if($where['city']){
    		$map['city'] = $where['city'];
    	}
    	
    	if($cat['city_id']&&empty($where['city'])){
    		$map['city']  = $cat['city_id'];
    	}
    	 
    	$map['isdel'] = 2;
    	 
    	$model = D("Drink");
    	$count = $model->count();
    	$list  = $model->where($map)->order($order.' '.$desc)
    	->page($nowPage.','.$pageNum)->select();
    	
		//dump($list);
    	//echo $model->getLastSql();exit;
    	
    	$data  = array("count"=>$count, "list"=>$list);
    	$this->success($data);
    }
    
    /**
     * 饮品列表，无分页
     */
    public function drinkList(){
    	$map['city']   = $this->_req("city","intval",0);
    	
    	$model = M($this->tableDrink);
    	$data  = $model->where($map)->select();
    	
    	$this->success($data);
    }
    
    /**
     * 库存商品领取
     */
    public function drinkGet(){
    	$param = array(
    		"before" => "drink_get_before",
    		"after"  => "drink_get_after"
    	);
    	parent::do_add("DrinkGet",$param);
    }
    
    /**
     * 库存商品领取记录
     */
    public function drinkGetList(){

    	$desc      = $this->_get("orderDirection","trim","desc");;
    	$order     = $this->_get("orderName","trim","addtime");
    	$pageNum   = $this->_get("pageNum","intval",C('PAGE_NUMS'));
    	$nowPage   = $this->_get("nowPage","intval",1);
    	$where     = $this->_req("where");
    	$package   = $this->_get("package","intval",0);
    	
    	if($where['drinkid']){
    		$map['drinkid'] = $where['drinkid'];
    	}
    	if($where['userid']){
    		$map['userid'] = $where['userid'];
    	}
    	if($where['startTime']){
    		$map['addtime'] = array("egt",$where['startTime']);
    	}
    	if($where['endTime']){
    		$map['addtime'] = array("elt",$where['endTime']);
    	}
    	
    	$model = D("DrinkGet");
    	$count = $model->count();
    	$list  = $model->where($map)->order($order.' '.$desc)
    	->page($nowPage.','.$pageNum)->select();
    	
    	//dump($where);
    	//echo $model->getLastSql();exit;
    	 
    	$data  = array("count"=>$count, "list"=>$list);
    	$this->success($data);
    }
    
    /**
     * 库存商品领取记录-无分页
     */
    public function drinkGetView(){
    	$where     = $this->_req("where");
    	 
    	if($where['drinkid']){
    		$map['drinkid'] = $where['drinkid'];
    	}
    	if($where['userid']){
    		$map['userid'] = $where['userid'];
    	}
    	if($where['startTime']){
    		$map['addtime'] = array("egt",$where['startTime']);
    	}
    	if($where['endTime']){
    		$map['addtime'] = array("elt",$where['endTime']);
    	}
    	 
    	$model = D("DrinkGet");
    	$count = $model->count();
    	$list  = $model->where($map)->select();
    	
    	$this->success($list);
    }
    
    /**
     * 所有饮品类型
     */
    public function drinkType(){
    	
    	$lang = getLang();
    	$data = getDictByKey("Dict_drink",$lang);
    	
    	$this->success($data);
    }
    
    /**
     * 获取城市的酒水饮料
     */
    public function drinkByCity(){
    	 
    	$map['city']  = $this->_req("city","intval",0);
		$map['isdel'] = 2;
    	$data = M($this->tableDrink)->where($map)->order("sort desc")->select();
    	$this->success($data);
    }
    
    /**
     * 添加饮品
     */
    public function drinkAdd(){
    	$param = array(
			"upload" => array("single"=>array("thumb")),
    		"before" => "drink_add_before",
    	);
    	parent::do_add("Drink",$param);
    }
    
    /**
     * 饮品详情
     */
    public function drinkDetail(){
    	
    	$id = $this->_req("id","intval",0);
    	
    	if($id){
    		
    		$data = M('Drink')->find($id);
			
			$data['single'] = find_thumb($data['solevar'],'thumb','Drink');
			
    		$this->success($data);
    	}else{
    		$this->error();
    	}
    }
    
    /**
     * 获取菜品分类
     */
    public function drinkbyType(){
    	 
    	$typeArr = $this->_req("type");
    	$lang    = getLang();
    	$data    = getDictByKey("Dict_drink",$lang);
    	$list    = $data['array'];
		
     	foreach($typeArr as $k=>$v){
    		foreach ($list as $m=>$n){
    			if($v==$m){
    				$n[3] = $m;
    				$Arr[$k]['type']  = $n;
    				$Arr[$k]['count'] = M($this->tableDrink)->where("type=".$m)->count();
    			}
    		}
    	} 
    	$this->success($Arr);
    }
    
    /**
     * 修改饮品
     */
    public function drinkEdit(){
    	$param = array(
			"upload" => array("single"=>array("thumb")),
    		"before" => "drink_add_before",
    	);
    	parent::do_edit("Drink",$param);
    }

    /**
     * 删除饮品
     */
    public function drinkDel(){
    	 
    	$val = $this->_req("val","trim","");
    	if($val){
    		 
    		$idArr = explode(',',$val);
    		foreach($idArr as $k=>$v){
    
    			$map['id']    = $v;
    			$map['isdel'] = 1;
    			$data = M('Drink')->save($map);
    		}
    	} 
    	if($data){
    		$this->success($data);
    	}else{
    		$this->error("请选择您要删除的饮品");
    	}
    }
    
    /**
     * 查询菜品分类（分页）
     */
    public function index(){
    	 
		$cat['city_id']   = $this->_req("city_id","intval",0);
    	$cat['addid']     = $this->_req("addid","intval",0);
    	$cat['role_view'] = $this->_req("role_view","intval",0);
		
		if($cat['role_view']==1){
			$map['d.addid'] = $cat['addid'];	
		}
		 
    	$desc     = $this->_req("orderDirection", "trim",   "desc");;
    	$order    = $this->_req("orderName",      "trim",   "addtime");
    	$pageNum  = $this->_req("pageNum",        "intval", C('PAGE_NUMS'));
    	$nowPage  = $this->_req("p",              "intval", 1);
    	$restid   = $this->_req("restid",         "intval", 0);
		$xiajia   = $this->_req("is_xiajia",      "intval", 0);
    	$where    = $_POST['where'];
    	
    	if($where){
    		foreach ($where as $k=>$v){
    			if($v){
					if($k == 'keywords'){
						$map['d.title|d.entitle|d.dishes_num'] = array('LIKE','%'.$v.'%');
					}elseif($k=="is_xiajia"){
						if($v==2){
							$map['is_xiajia'] = array("neq",1);
						}else{
							$map['is_xiajia'] = 1;
						}
					}else{
						$map['d.'.$k] = $v;
					}    				
    			}
    		}
    	}
    	
    	if($restid&&empty($map['d.restid'])){
    		$map['d.restid'] = $restid;
    	}

    	if($cat['city_id']){
    		$map['s.city']  = $cat['city_id'];
    	}

		if($xiajia){
			$map['is_xiajia'] = $xiajia;
		}
    	
    	$map['d.isdel'] = 2 ;
    	$con      = $order.' '.$desc;
    	$table    = $this->tableOutletDishesView; 
		
    	$this->success(parent::pageSelect($table,$map,$pageNum,$nowPage,$con));
    }
    
    /**
     * 查询餐厅菜品（无分页）
     */
    public function dview(){
    	
    	$restid = $this->_req("restid");
    	
    	if($restid){
    		$map['s.id'] = $restid;
    	}
    	$map['d.isdel']  = 2;
    	
    	$data = D($this->tableOutletDishesView)->where($map)->order('dishes_num desc')->select();
    	$this->success($data);
    } 
    
    /**
     * 条件查询菜品（无分页）
     */
    public function dishByCondition(){
    	
    	$restid   = $this->_get("restid", "intval", 0);
    	 
    	//定义条件数组map
    	$map['isdel']     = 2 ;
    	$map['is_xiajia'] = 2;
    	if($restid) $map['restid'] = $restid ;
    	
    	$data = M($this->tableDishes)->where($map)->order("addtime desc")->select();
    	$this->success($data);
    }
    
    /**
     * 根据销量获取前10条数据
     */
    public function topTen(){
    	 
    	$restid   = $this->_get("restid", "intval", 0);
    
    	//定义条件数组map
    	$map['isdel']     = 2 ;
    	$map['is_xiajia'] = 2 ;
    	$map['restid']    = $restid ;
    	$data['data'] = M($this->tableDishes)->where($map)->order("ordernum desc")->limit(10)->select();
    	$Arr          = M($this->tableDishes)->where($map)->select();
    	$category     = M($this->tableDishsort)->where('restid='.$map['restid'])->order('sort desc')->select();
    	
    	foreach($category as $m=>$n){
    		foreach($Arr as $k=>$v){
    			if($v['sortid']==$n['id']){
    				$sortidArr[] = $v['sortid'];
    			}
    		}
    	}
    	$data['sortid'] = $sortidArr;
    	$this->success($data);
    }
    
    /**
     * 添加菜品
     */
    public function add(){
    	$param = array(
				"upload" => array("single"=>array("thumb")),
    			"before"   => "dishes_add_before",
    			"return"  => true
    	);
    
    	parent::do_add($this->tableDishes,$param);
    
    }
    /**
     * 修改菜品
     */
    public function edit(){
    
    	$param = array(
			"upload" => array("single"=>array("thumb")),
    		"before" => "dishes_add_before",
    		"return" => true
    	);
    	parent::do_edit($this->tableDishes,$param);
    
    }
    
    /**
     * 查询菜品详情
     */
    public function dishesDetail(){
    
    	$id = $this->_get("id","intval",0);
    	$data  = M($this->tableDishes)->find($id);
		//F('upload',find_thumb($data['solevar'],'thumb',$this->tableDishes));
		//查找缩略图
		$data['single'] = find_thumb($data['solevar'],'thumb',$this->tableDishes);
		
    	$this->success($data);
    
    }
    
    /**
     * 查询餐厅菜品关联详情
     */
     public function detail(){
    
    	$id = $this->_get("id","intval",0);
    	$data  = D($this->tableOutletDishesView)->where("d.id=".$id)->find();
		
		//查找缩略图
		$data['single'] = find_thumb($data['solevar'],'thumb',$this->tableDishes);
		
    	$this->success($data);
    } 
    
    /**
     * 批量复制商品属性
     */
    public function fuzhi(){
    	
    	$id   = $this->_req("id");
    	$val  = $this->_req("val");
    	$view = M("Dishes")->find($id);
    	
    	if($view['id']){
    		if($val){
    			$idArr = explode(',',$val);
    			foreach($idArr as $k=>$v){
    		
    				$map['id']    = $v;
    				$map['gnfr']  = $view['gnfr'];
    				$data = M($this->tableDishes)->save($map);
    			}
    			if($data){
    				$this->success($data);
    			}else{
    				$this->error("菜品属性复制失败");
    			}
    		}else{
    			$this->error("菜品属性复制失败，请选择需要复制属性的菜品!");
    		}
    	}else{
    		$this->error("菜品属性复制失败，没有选择复制源!");	
    	}
    }
    
    /**
     * 删除菜品
     */
    public function del(){

		$id   = $this->_get("id","intval",0);
    	$maps = array("id"=>$id,"isdel"=>1);
    	$data = M($this->tableDishes)->save($maps);
    	
    	//查询该菜品分类下是否还有其他菜品，如果为空，则删除该分类
    	$one   = M($this->tableDishes)->find($data);
    	$where = array('sortid'=>$one['sortid']);
    	$list  = M($this->tableDishes)->where($where)->select();
    	if(empty($list)){
    		$map['id'] = $one['sortid'];
    		M($this->tableDishsort)->where($map)->delete();
    	}
    	$this->success($data);
    }
    
    /**
     * 批量删除菜品
     */
    public function delall(){
    	 
    	$val = $this->_req("val","trim","");
    	if($val){
    		$idArr = explode(',',$val);
    		foreach($idArr as $k=>$v){
    			 
    			$map['id']    = $v;
    			$map['isdel'] = 1;
    			$data = M($this->tableDishes)->save($map);

				//查询该菜品分类下是否还有其他菜品，如果为空，则删除该分类
    			$one   = M($this->tableDishes)->find($data);
    			$where = array('sortid'=>$one['sortid']);
    			$list  = M($this->tableDishes)->where($where)->select();
    			if(empty($list)){
    				$map['id'] = $one['sortid'];
    				M($this->tableDishsort)->where($map)->delete();
    			}
    		}
    	}
    	if($data){
    		$this->success($data);
    	}else{
    		$this->error("请选择您要删除的菜品");
    	}
    }
    
    /**
     * 菜品批量上下架
     */
    public function is_xsj(){
    
    	$val = $this->_req("val","trim","");
    	$sxj = $this->_req("sxj");
    	$msg = "批量操作失败";
    	
    	if($sxj==1){
    		$msg = "批量操作失败，当前没有菜品需要批量下架操作";
    	}else{
    		$msg = "批量操作失败，当前没有菜品需要批量上架操作";
    	}
    	
    	if($val){
    		$idArr = explode(',',$val);
    		foreach($idArr as $k=>$v){
    
    			$map['id']        = $v;
    			$map['is_xiajia'] = $sxj;
    			$data = M($this->tableDishes)->save($map);
    		}
    	}
    	if($data){
    		$this->success($data);
    	}else{
    		$this->error($msg);
    	}
    }
    
    /**
     * 菜品批量上下架
     */
    public function isshow(){
    
    	$val    = $this->_req("val","trim","");
    	$isshow = $this->_req("isshow");
    	$msg    = "批量操作失败";
    	
    	if($isshow==1){
    		$msg = "批量操作失败,当前没有需要批量显示的库存商品";
    	}else{
    		$msg = "批量操作失败,当前没有需要批量隐藏的库存商品";
    	}
    	
    	if($val){
    		$idArr = explode(',',$val);
    		foreach($idArr as $k=>$v){
    
    			$map['id']     = $v;
    			$map['isshow'] = $isshow;
    			$data = M($this->tableDrink)->save($map);
    		}
    	}
    	if($data){
    		$this->success($data);
    	}else{
    		$this->error($msg);
    	}
    }
    
    /**
     * 菜品分类有子类
     */
    public function view(){
    	 
    	$map = array("isdel"=>2,"issub"=>1);
    	$this->success(M($this->tabltableDisheseDishsort)->where($map)->select());
    
    }
    
    /**
     * 菜品分类无分页查询
     */
    public function select(){
    
    	$map = array("isdel"=>2);
    	 
    	$this->success(M($this->tableDishsort)->where($map)->select());
    
    }
    
    /**
     * 根据分类编号获取菜品分类
     */
    public function bySortid(){
    	
    	$idArr = $this->_req("sortid");
    	foreach($idArr as $k=>$v){
    		
    		$sort  = M($this->tableDishsort)->where("id=".$v)->find();
			$count = M($this->tableDishes)->where("sortid=".$v." and isdel=2")->count();

    		if($sort&&$count){
	    		$data[$k]['sort']  = $sort;
	    		$data[$k]['count'] = $count;
    		}
    	}
    	
    	$this->success($data);
    }
    
    
    /**
     * 根据id查询
     */
    public function find(){
    
    	$id = $this->_get("id","intval",0);
    
    	$this->success(M($this->tableDishsort)->find($id));
    
    }
    
    /**
     * 查询菜品分类（分页）
     */
    public function sort(){
    	
    	$restid = $this->_get("restid","intval",0);
    	
    	if($restid){
    		$map  = array("isdel"=>2,"restid"=>$restid);
    	}else{
    		$map  = array("isdel"=>2);
    	}
    	
    	$data = M($this->tableDishsort)->where($map)->select();
    	
    	$this->success($data);
    
    }
    /**
     * 添加菜品分类
     */
    public function sortAdd(){
    	 
    	parent::do_add($this->tableDishsort);
    	 
    }
    
    /**
     * 修改菜品分类
     */
    public function sortEdit(){
    
    	parent::do_edit($this->tableDishsort);
    
    }
    
    /**
     * 删除菜品分类
     */
    public function sortDel(){
    
    	$id = $this->_get("id","intval",0);
    
    	$res = M($this->tableDishsort)->where("pid=".$id)->find();
    	if($res){
    		$this->error("该分类内还有子类别，不能删除！");
    	}else{
    		parent::do_del($this->tableDishsort,true);   //删除菜品分类表中相关数据
    	}
    
    }
    
    /**
     * 查询餐厅内菜品分类
     */
    public function sortView(){
    	
    	$restid = $this->_get("restid","intval",0);
    	$map    = array("isdel"=>2,"restid"=>$restid);
    	$data = M($this->tableDishsort)->where($map)->select();
    	$this->success($data);
    
    }
    
    /**
     * 根据父类查询子类
     */
    public function selSub(){
    	
    	$model = M($this->tableDishsort);
    	
    	$restid = $this->_get("restid","intval",0);
    	$map    = array("isdel"=>2,"issub"=>1,"restid"=>$restid);
    	$parent = $model->where($map)->select();
    	
    	foreach($parent as $k=>$v){
    		
    		if($v['issub']==1){
    			$arr['pid'] = $v['id'];
    			$sub = $model->where($arr)->select();
    			$list[$k]['parent'] = $v;
    			$list[$k]['child']  = $sub;
    		}
    	}
    	$this->success($list);
    	
    }
    
    
    /**
     * 查菜品分类是否有子类
     */
    public function sortIssub(){
    	 
    	$pid    = $this->_get("pid","intval",0);
    	$map    = array("isdel"=>2,"pid"=>$pid);
    	$this->success(M($this->tableDishsort)->where($map)->select());
    
    }
    
    /**
     * 菜品分类无分页查询
     */
    public function sortSelect(){
    
    	$map = array("isdel"=>2);
    	 
    	$this->success(M($this->tableDishsort)->where($map)->select());
    
    }
    
    /**
     * 根据id查询
     */
    public function sortFind(){
    
    	$id = $this->_get("id","intval",0);
    
    	$this->success(M($this->tableDishsort)->find($id));
    
    }
    
}